package be.redo.controller;

import be.redo.model.entities.AirlineCompany;
import be.redo.service.AirlineCompanyService;
import be.redo.service.FlightService;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

@RequestMapping("/airlinecompanys")
@Controller
public class AirlineCompanyController {

	@Autowired
	AirlineCompanyService airlineCompanyService;

	@Autowired
	FlightService flightService;

	@RequestMapping(method = RequestMethod.POST, produces = "text/html")
	public String create(@Valid AirlineCompany airlineCompany,
			BindingResult bindingResult, Model uiModel,
			HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			populateEditForm(uiModel, airlineCompany);
			return "airlinecompanys/create";
		}
		uiModel.asMap().clear();
		airlineCompanyService.create(airlineCompany);
		return "redirect:/airlinecompanys/"
				+ encodeUrlPathSegment(airlineCompany.getAirlineCompanyId()
						.toString(), httpServletRequest);
	}

	@RequestMapping(params = "form", produces = "text/html")
	public String createForm(Model uiModel) {
		populateEditForm(uiModel, new AirlineCompany());
		return "airlinecompanys/create";
	}

	@RequestMapping(value = "/{AirlineCompanyId}", produces = "text/html")
	public String show(@PathVariable("AirlineCompanyId") Long AirlineCompanyId,
			Model uiModel) {
		uiModel.addAttribute("airlinecompany",
				airlineCompanyService.read(AirlineCompanyId));
		uiModel.addAttribute("itemId", AirlineCompanyId);
		return "airlinecompanys/show";
	}

	@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
	public String update(@Valid AirlineCompany airlineCompany,
			BindingResult bindingResult, Model uiModel,
			HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			populateEditForm(uiModel, airlineCompany);
			return "airlinecompanys/update";
		}
		uiModel.asMap().clear();
		airlineCompanyService.update(airlineCompany);
		return "redirect:/airlinecompanys/"
				+ encodeUrlPathSegment(airlineCompany.getAirlineCompanyId()
						.toString(), httpServletRequest);
	}

	@RequestMapping(value = "/{AirlineCompanyId}", params = "form", produces = "text/html")
	public String updateForm(
			@PathVariable("AirlineCompanyId") Long AirlineCompanyId,
			Model uiModel) {
		populateEditForm(uiModel, airlineCompanyService.read(AirlineCompanyId));
		return "airlinecompanys/update";
	}

	@RequestMapping(value = "/{AirlineCompanyId}", method = RequestMethod.DELETE, produces = "text/html")
	public String delete(
			@PathVariable("AirlineCompanyId") Long AirlineCompanyId,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size,
			Model uiModel) {
		AirlineCompany airlineCompany = airlineCompanyService
				.read(AirlineCompanyId);
		airlineCompanyService.delete(airlineCompany.getAirlineCompanyId());
		uiModel.asMap().clear();
		uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
		uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
		return "redirect:/airlinecompanys";
	}

	void populateEditForm(Model uiModel, AirlineCompany airlineCompany) {
		uiModel.addAttribute("airlineCompany", airlineCompany);
		uiModel.addAttribute("flights", flightService.findAll());
	}

	String encodeUrlPathSegment(String pathSegment,
			HttpServletRequest httpServletRequest) {
		String enc = httpServletRequest.getCharacterEncoding();
		if (enc == null) {
			enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
		}
		try {
			pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
		} catch (UnsupportedEncodingException uee) {
		}
		return pathSegment;
	}
}
